<template>
	<view class="viewcontent">
		<view class="viewcontenthead">
			<view class="fl" @click="$UTILS.goBack(1)"></view>
			<view class="fr" style="color: #fdf5fa;"> 物资清点</view>
			<view class="leftitem" @click="selecteditem">
				<image src="../../../static/rwcj.svg" style="height:32rpx;width: 32rpx;"></image>
			</view>
		</view>
		<view class="viewcontenmes">
			<view class="searchitem">
				<view class="easyinputmes"><uni-easyinput v-model="value" placeholder="请输入内容"
						@input="input"></uni-easyinput></view>
				<view class="easyinputimg">
					<image @click="searchdemo" src="../../../static/Search.svg" style="height:52rpx;width: 52rpx;">
					</image>
				</view>

			</view>
			<view class="listitem">
				<view class="listitemdemo" v-for="(item,index) in this.itemlist" :key="index">
					<view class="listitemdemoitem1">
						<view class="listitemdemoitem1item1">
							<view>清点时间：{{item.data}}</view>
							<view>
								<image @click="sctask(item,index)" src="../../../static/sc.svg"
									style="height:52rpx;width: 52rpx;"></image>
							</view>
						</view>
						<view class="listitemdemoitem1item2">
							<view>
								<view class="overflowtext">任务名称：{{ item.taskName }}</view>
							</view>
						</view>
					</view>
					<view class="listitemdemoitem2">
						<view class="listitemdemoitem2item1">
							<view class="listitemdemoitem2item1mes1">清点名称：{{item.taskCode}}</view>
							<view class="listitemdemoitem2item1mes2">清点状态：{{item.taskStatus==0?'部分丢失':'正常'}}</view>
						</view>
						<view class="listitemdemoitem2item2">
							<u-button @click.native.stop="redirecttaskmessage(index)">查看详情</u-button>
						</view>
					</view>
				</view>
			</view>

		</view>
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" title="输入内容" @confirm="dialogInputConfirm">
				<uni-easyinput v-model="taskinputvalue" placeholder="请输入内容" @input="input1"></uni-easyinput>
			</uni-popup-dialog>
		</uni-popup>
		<myLoading v-if="loading" style="position: absolute;z-index: 9;"></myLoading>
	</view>
</template>

<script>
	export default {
		onShow() {
			this.loading = true
			if (this.$UTILS.getitemlist()) {
				let item = this.$UTILS.getitemlist().filter(item => {
					return item.taskCode == this.$UTILS.gettaskCode()
				})
				this.titlemessage = item[0].taskName
				this.taskCode = item[0].taskCode
				this.taskId = item[0].taskId
				this.createBy = item[0].createBy
			} else {
				this.loading = false
				this.$UTILS.toast('没有当前任务')
			}
			// let item = this.$UTILS.getitemlist().filter(item=>{return item.taskCode == this.$UTILS.gettaskCode() }) 
			// this.titlemessage = item[0].taskName
			// this.taskId = item[0].taskId
			// this.createBy = item[0].createBy
			setTimeout(() => {
				this.loading = false
				this.itemlist = this.$UTILS.getInventorysupplies()
				if (this.itemlist.length > 0) {
					this.itemlist = this.itemlist.map(item => {
						let de = item.equipmentList.find(ite => {
							return ite.equipmentStatus != 1
						})
						if (de) {
							item.taskStatus = 0
						} else {
							item.taskStatus = 1
						}
						return item
					})
				}


			}, 1000)
		},
		data() {
			return {
				value: '',
				itemlist: [],
				selectindex: 0,
				taskCode: 0,
				loading: false,
				taskinputvalue: '',
				titlemessage: '',
				taskId: 0,
				createBy: "",
				flag: true
			}
		},
		methods: {
			async sctask(item, index) {
				let takingOrderDTO = {}
				takingOrderDTO.takingName = item.taskCode
				takingOrderDTO.taskCode = this.taskCode
				takingOrderDTO.takingPerson = this.createBy
				takingOrderDTO.taskId = this.taskId
				takingOrderDTO.itemList = item.equipmentList.reduce((arr, cur) => {
					let obj = {}
					obj.suppliesId = cur.equipmentId || cur.boxId
					obj.suppliesStatus = cur.equipmentStatus
					arr.push(obj)
					return arr
				}, [])
				this.loading = true
				let [error, res] = await this.$UTILS.requestRest(this.$COMMON.REST_URL.TAKINGORDER, 'POST', JSON.stringify(takingOrderDTO))
				if (res && res.statusCode == 200) {
					if (res.data?.code == 401) {
						let jqdizhi = this.$UTILS.getunlinelogin()
						let ictdizhi = this.$UTILS.getunlineaddress()
						uni.clearStorageSync()
						this.$UTILS.setunlinelogin(jqdizhi);
						this.$UTILS.setunlineaddress(ictdizhi);
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/view-layout/login/login',
							});
						}, 1000)
						this.$UTILS.toast('token过期，请从新登录')
						return
					}
					this.$UTILS.toast('数据提交成功')
					this.loading = false
				} else {
					this.$UTILS.toast('数据提交失败')
					this.loading = false
				}
			},
			input(e) {
				this.value = e
			},
			input1(e) {
				this.taskinputvalue = e
			},
			// searchdemo(){

			// 	   if(this.value == ''){
			// 		  this.itemlist = this.$UTILS.getitemlist()
			// 		  return
			// 	   }
			// 		this.itemlist = this.$UTILS.getitemlist().filter(item=>{
			// 			return item.taskName.includes(this.value)
			// 		})
			// },
			dialogInputConfirm(val) {
				let obj = this.$UTILS.getInventorysupplies() || []
				let demo = {}
				const now = new Date();
				const year = now.getFullYear();
				const month = now.getMonth() + 1; // getMonth() 返回的月份是从0开始的，所以需要+1
				const day = now.getDate();
				const hours = now.getHours();
				const minutes = now.getMinutes();
				const seconds = now.getSeconds();
				const formatNumber = (n) => n < 10 ? '0' + n : n;
				const formattedDateTime =
					`${year}-${formatNumber(month)}-${formatNumber(day)} ${formatNumber(hours)}:${formatNumber(minutes)}:${formatNumber(seconds)}`;
				demo.data = formattedDateTime
				demo.taskName = this.titlemessage
				if (this.taskinputvalue != '') {
					demo.taskCode = this.taskinputvalue
					demo.taskStatus = 0
					demo.equipmentList = this.$UTILS.getitemlist().filter(item => {
						return item.taskCode == this.$UTILS.gettaskCode()
					})[0].equipmentList
					demo.equipmentList = demo.equipmentList.map(item => {
						item.describe = ''
						item.equipmentStatus = 0
						return item
					})
					obj.push(demo)
					this.$UTILS.setInventorysupplies(obj)
					this.itemlist = this.$UTILS.getInventorysupplies()
					this.taskinputvalue = ''

				} else {
					demo.taskCode = '任务名称' + ((obj.length + 1) * 1)
					demo.taskStatus = 0
					demo.equipmentList = this.$UTILS.getitemlist().filter(item => {
						return item.taskCode == this.$UTILS.gettaskCode()
					})[0].equipmentList
					demo.equipmentList = demo.equipmentList.map(item => {
						item.describe = ''
						item.equipmentStatus = 0
						return item
					})
					obj.push(demo)
					this.$UTILS.setInventorysupplies(obj)
					this.itemlist = this.$UTILS.getInventorysupplies()
					this.taskinputvalue = ''
				}
				this.itemlist = this.$UTILS.getInventorysupplies()
				this.$refs.inputDialog.close()

			},
			selecteditem() {
				if (!this.$UTILS.getitemlist()) {
					this.$UTILS.toast('没有当前任务，无法创建清点单')
					setTimeout(() => {
						uni.navigateBack();
					}, 1000)

					return
				}
				this.$refs.inputDialog.open()
				/*
				let command ={
					appCode: 'ict'
				}
				let [err,res] = await this.$UTILS.requestRestgetnoparam(this.$COMMON.REST_URL.TASKLIST, 'GET', command)
				this.itemlist = res.data.result
				
				this.$UTILS.setitemlist(this.itemlist);
				*/

			},
			searchdemo() {

				if (this.value == '') {
					this.itemlist = this.$UTILS.getInventorysupplies()
					return
				}
				this.itemlist = this.$UTILS.getInventorysupplies().filter(item => {
					return item.taskCode.includes(this.value)
				})
			},
			checkedselect(item) {
				this.loading = true
				this.taskCode = item.taskCode
				this.$UTILS.settaskCode(this.taskCode)
				setTimeout(() => {
					this.loading = false
				}, 1000)

			},
			redirecttaskmessage(index) {
				let data = this.itemlist[index]
				if (data.equipmentList <= 0) {
					this.$UTILS.toast('没有设备')
					return
				}
				uni.navigateTo({
					url: `/pages/view-working/Devicestatus/Devicestatus?data=${JSON.stringify(data)}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.viewcontent {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100vh;
		box-sizing: border-box;
		padding-top: 55rpx;

		.viewcontenthead {
			width: 100%;
			height: 5%;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.fl {
				width: 100rpx;
				height: 10vh;
				line-height: 10vh;
				background: url("/static/right-arrow.png") no-repeat center;
				background-size: 14%;
				transform: rotate(180deg);
				cursor: pointer; // 增加鼠标指针样式
			}

			.fr {
				cursor: pointer; // 增加鼠标指针样式
			}

			.leftitem {
				width: 100rpx;
				display: flex;
				justify-content: center;
				z-index: 999;
			}
		}

		.viewcontenmes {
			width: 100%;
			height: 95%;

			.searchitem {
				width: 100%;
				height: 10%;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: space-between;

				::v-deep .uni-input-input {
					color: rgba(250, 249, 249, 0.8) !important;
				}

				.easyinputmes {
					width: 660rpx;
					box-sizing: border-box;
					padding: 0 25rpx;
				}

				.easyinputimg {
					width: 80rpx;
				}
			}

			.listitem {
				width: 100%;
				height: 90%;
				overflow: auto;
				display: flex;
				flex-direction: column;

				.listitemdemo {
					border: 1px solid white;
					border-radius: 15px;
					width: 98%;
					height: 250rpx;
					margin: 0 auto;
					margin-top: 15rpx;
					box-sizing: border-box;
					padding: 0 5rpx;
					font-family: PingFang SC;
					font-weight: 550;
					color: rgba(250, 249, 249, 0.8);
					font-size: 16px;

					.listitemdemoitem1 {
						width: 100%;
						height: 50%;

						.listitemdemoitem1item1 {
							width: 100%;
							height: 50%;
							display: flex;
							box-sizing: border-box;
							padding-right: 25rpx;
							padding-left: 55rpx;
							justify-content: space-between;
							align-items: center;
						}

						.listitemdemoitem1item2 {
							width: 100%;
							height: 50%;
							box-sizing: border-box;
							padding-left: 55rpx;
							display: flex;
							align-items: center;

							.overflowtext {

								overflow: hidden;
								white-space: nowrap;
								text-overflow: ellipsis;
							}
						}
					}

					.listitemdemoitem2 {
						width: 100%;
						height: 50%;
						display: flex;
						flex-direction: row;

						.listitemdemoitem2item1 {
							width: 75%;
							height: 100%;

							.listitemdemoitem2item1mes1 {
								width: 100%;
								height: 50%;
								box-sizing: border-box;
								padding-left: 55rpx;
								display: flex;
								align-items: center;
							}

							.listitemdemoitem2item1mes2 {
								width: 100%;
								height: 50%;
								box-sizing: border-box;
								padding-left: 55rpx;
								display: flex;
								align-items: center;
							}
						}

						.listitemdemoitem2item2 {
							width: 25%;
							height: 100%;
							display: flex;
							align-items: center;
						}
					}
				}
			}
		}
	}

	::v-deep .uni-popup__error {
		color: rgba($color: #6a6265, $alpha: 1.0);
	}
</style>